package com.example.flower_shop.controller.usr;

import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.service.usr.StoreOrderService;
import com.example.flower_shop.vo.order.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户订单操作")
@RestController
@RequestMapping("/api/usr/order")
public class OrderController {

    @Resource
    private StoreOrderService orderService;

    @GetMapping("/list")
    @Operation(summary = "订单列表", method = "GET")
    public PageResult<UsrOrderPageResp> orderList(@Valid UsrOrderPageReq req) {
        return orderService.orderList(req);
    }

    @GetMapping("/info/{orderId}")
    @Operation(summary = "查看订单详情", method = "GET",
            parameters = {
                    @Parameter(name = "orderId", description = "订单ID", required = true, in = ParameterIn.PATH)
            })
    public List<AdmOrderDetailResp> getOrderInfo(@PathVariable Long orderId) {
        return orderService.getOrderDetail(orderId);
    }

    @GetMapping("/cancel/{orderId}")
    @Operation(summary = "取消订单", method = "GET",
            parameters = {
                    @Parameter(name = "orderId", description = "订单ID", required = true, in = ParameterIn.PATH)
            })
    public void cancelOrder(@PathVariable Long orderId) {
        orderService.cancelOrder(orderId);
    }

    @GetMapping("/take/{orderId}")
    @Operation(summary = "确认收货", method = "GET",
            parameters = {
                    @Parameter(name = "orderId", description = "订单ID", required = true, in = ParameterIn.PATH)
            })
    public void takeOrder(@PathVariable Long orderId) {
        orderService.takeOrder(orderId);
    }

    @PostMapping("/pre/order")
    @Operation(summary = "预下单", method = "POST", description = "订单预下单，返回结算信息等数据",
            requestBody = @RequestBody(
                    content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
    public PreOrderResp preOrder(@Valid PreOrderReq req) {
        return orderService.preOrder(req);
    }

    @GetMapping("/pre/order/load/{preOrderNo}")
    @Operation(summary = "加载预下单信息", method = "POST",
            parameters = {
                    @Parameter(name = "preOrderNo", description = "预下单单号", required = true, in = ParameterIn.PATH)
            })
    public PreOrderResp preOrderLoad(@PathVariable String preOrderNo) {
        return orderService.preOrderLoad(preOrderNo);
    }

    @PostMapping("/payOrder")
    @Operation(summary = "下单并支付")
    public OrderPrepayResp payOrder(@Valid @org.springframework.web.bind.annotation.RequestBody PreOrderReq req) {
        return orderService.payOrder(req);
    }


    @PostMapping("/create")
    @Operation(summary = "创建订单", method = "POST",
            requestBody = @RequestBody(
                    content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
    public CreateOrderResp createOrder(@Valid CreateOrderReq req) {
        return orderService.createOrder(req);
    }

    @PostMapping("/pre/pay")
    @Operation(summary = "订单预支付", method = "POST",
            requestBody = @RequestBody(
                    content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
    public OrderPrepayResp prepayOrder(@Valid PrePayOrderReq req) {
        return orderService.prepayOrder(req);
    }

    @PostMapping("/refund")
    @Operation(summary = "订单退款", method = "POST",
            requestBody = @RequestBody(
                    content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
    public void orderRefund(@Valid OrderRefundReq req) {
        orderService.orderRefund(req);
    }
}
